#######################################################################
# Motivated Corporate Political Action: Evidence from an SEC Experiment
# Mary Kroeger and Maria Silfa
#######################################################################

rm(list=ls())

# Load packages
library(foreign)
library(ggplot2)
library(lfe)
library(ggpubr)


######## Main results

load("main_data.RData")

## Figure 1
year_lobby <- aggregate(lobby~pilot+year,main_data,mean)

year_lobby$treatment <- ifelse(year_lobby$pilot==1,"treated","control")


p1 <- ggplot(year_lobby, aes(year, lobby, group = treatment)) +
  geom_point() + geom_line(aes(linetype = treatment)) +
  geom_vline(xintercept = "2005", linetype = 2) +
  theme_bw(base_family = "serif", base_size = 12) +
  theme(plot.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill = "white"),
        legend.title = element_blank()) +
  theme(legend.position = "none") +
  labs(x = "Year", y = "Lobbying Activity", title="Figure 1: Effect of Pilot on Lobbying")


## Figure 2
year_contri <- aggregate(contri~pilot+year,main_data,mean)

year_contri$treatment <- ifelse(year_contri$pilot==1,"treated","control")

p2 <- ggplot(year_contri, aes(year, contri, group = treatment)) +
  geom_point() + geom_line(aes(linetype = treatment)) +
  geom_vline(xintercept = "2005", linetype = 2) +
  theme_bw(base_family = "serif", base_size = 12) +
  theme(plot.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill = "white"),
        legend.title = element_blank()) +
  theme(legend.position="none") +
  labs(x = "Year", y = "Campaign Contributor", title="Figure 2: Effect of Pilot on Campaign Contributions")

p1 <- p1 + theme(legend.position="none")

# Combine plots
p <- ggarrange(p1, p2, common.legend = TRUE, legend="right", nrow=2)
ggsave(filename="plots.pdf", plot=p, width = 6, height = 5.29, units = "in", dpi = 300)


## Table 1
m1 <- felm(lobby~treated|ticker+year|0|ticker,main_data)
m2 <- felm(contri~treated|ticker+year|0|ticker,main_data)


######## Appendix B


## Table B1

##
# Total number of firms
length(unique(main_data$ticker)) #2,835

# Treated
aggregate(treated ~ year, data=main_data, FUN=sum)[4,2] #943

# Control
aggregate(treated==0 ~ year, data=main_data, FUN=sum)[4,2] #1,892

##
# Exchanges
# NYSE or AMEX 
aggregate(exch=="NYSE" ~ year, data=main_data, FUN=sum)[1,2] #1,491

# Nasdaq
aggregate(exch=="NNM" ~ year, data=main_data, FUN=sum)[1,2] #1,344

##
# Political Engagement
# Fraction that lobby
l <- aggregate(lobby ~ ticker, data=main_data, sum)
round(prop.table(table(l$lobby>0))[2],2) #0.39

# Fraction that donate
d <- aggregate(contri ~ ticker, data=main_data, sum)
round(prop.table(table(d$contri>0))[2],2) #0.15

# Fraction that donate and lobby
ld <- merge(l,d, by="ticker")
round(prop.table(table(ld$lobby > 0 & ld$contri > 0)),2)

## Table B2
pre_lobby <- felm(lobby~t_2003+t_2004+t_2005+t_2006+t_2007|ticker+year|0|ticker,main_data)
pre_contri <- felm(contri~t_2003+t_2004+t_2005+t_2006+t_2007|ticker+year|0|ticker,main_data)


## Table B3
ceo1 <- felm(ceo_contri~treated|ticker+year|0|ticker,main_data)
ceo2 <- felm(log_ceo~treated|ticker+year|0|ticker,main_data)
ceo3 <- felm(ceo_comm~treated|ticker+year|0|ticker,main_data)


## Table B4
lobby_2000 <- felm(lobby~treated|ticker+year|0|ticker,main_data,subset=(index==2000))
contri_2000 <- felm(contri~treated|ticker+year|0|ticker,main_data,subset=(index==2000))


## Table B5
exp1 <- felm(log_lobby~treated|ticker+year|0|ticker,main_data)
exp2 <- felm(log_contri~treated|ticker+year|0|ticker,main_data)


## Table B6
exch1 <- felm(lobby~treated|ticker+year_exch|0|ticker,main_data)
exch2 <- felm(contri~treated|ticker+year_exch|0|ticker,main_data)


## Table B7
load("period_data.RData")

period1 <- felm(lobby~treated|ticker+period|0|ticker,period_data,subset=(period>1 & period<12))
period2 <- felm(contri~treated|ticker+period|0|ticker,period_data,subset=(period>1 & period<12))


## Table B8 
load("announcement_data.RData")

announce1 <- felm(lobby~pilot*treatment,announcement_data)
announce2 <- felm(contri~pilot*treatment,announcement_data)


## Table B9
sec <- felm(lobby_sec~treated|ticker+year|0|ticker,main_data)
cong <- felm(lobby_cong~treated|ticker+year|0|ticker,main_data)


## Table B10 
comm <- felm(contri_comm~treated|ticker+year|0|ticker,main_data)
